﻿/***********************************************************************
 * <copyright file="NewsRequestHelper.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:  Nguyen Chi Binh
 * Email:    nguyentribinh@live.com
 * Website:
 * Create Date: Friday, April 11, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Configuration;
using System.Web;
using DXWebApplication.NewsServiceReference;
using DXWebApplication.Repositories.Implementations;

namespace DXWebApplication.Repositories.Core.News
{
    public static class NewsRequestHelper
    {
        // The application ClientTag 
        public static string ClientTag { get; private set; }

        /// <summary>
        /// Static constructor. Sets the ClientTag (read from web.config).
        /// </summary>
        static NewsRequestHelper()
        {
            ClientTag = ConfigurationManager.AppSettings.Get("ClientTag");
        }

        /// <summary>
        /// Gets or sets the Access Token value (provided by Server and stored in Session).
        /// </summary>
        public static string AccessToken
        {
            get
            {
                if (HttpContext.Current.Session["NewsAccessToken"] == null)
                {
                    // Request a unique accesstoken from the webservice. This token is
                    // valid for the duration of the session.
                    var newRepository = new NewsAuthRepository();
                    HttpContext.Current.Session["NewsAccessToken"] = newRepository.GetToken();
                }
                return (string)HttpContext.Current.Session["NewsAccessToken"];
            }
        }

        /// <summary>
        /// Gets the user identifier.
        /// </summary>
        /// <value>
        /// The user identifier.
        /// </value>
        public static string UserName
        {
            get
            {                
                return HttpContext.Current.Session["UserId"]!=null ? HttpContext.Current.Session["UserId"].ToString() : null;
            }
        }

        /// <summary>
        /// Helper extension method that adds RequestId, ClientTag, and AccessToken to all request types.
        /// </summary>
        /// <typeparam name="T">The request type.</typeparam>
        /// <param name="request">The request</param>
        /// <returns>Fully prepared request, ready to use.</returns>
        public static T Prepare<T>(this T request) where T : RequestBase
        {            
            request.RequestId = RequestId;
            request.ClientTag = ClientTag;
            request.AccessToken = AccessToken;            

            return request;
        }

        /// <summary>
        /// Generates unique request GUID identifier. 
        /// </summary>
        public static string RequestId
        {
            get { return Guid.NewGuid().ToString(); }
        }
    }
}